//
//  KBTabBarPlayButton.m
//  喜马拉雅改良版框架
//
//  Created by dllo on 15/12/24.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "KBTabBarPlayButton.h"
#import "CoreDataManager.h"

@implementation KBTabBarPlayButton
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self createSubViews];
        _managerObjectContext = [[CoreDataManager shareManager]managedObjectContext];
    }
    return self;
}

- (void)createSubViews
{
    self.backgroundColor = [UIColor clearColor];
    
    self.frame = CGRectMake(SCREEN_WIDTH*0.42, -SCREEN_WIDTH*0.06, SCREEN_WIDTH*0.18, SCREEN_HEIGHT*0.18);
    self.userInteractionEnabled = YES;
    
    // 背景
    self.btnBackground = [[UIImageView alloc] initWithFrame:self.bounds];
    self.btnBackground.backgroundColor = [UIColor colorWithWhite:0.995 alpha:1.000];
    self.btnBackground.layer.masksToBounds = YES;
    self.btnBackground.layer.cornerRadius = SCREEN_WIDTH*0.09;
    [self addSubview:self.btnBackground];
    self.btnBackground.tag = 1000;
    
    //条
    UIImageView *tiao = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH*0.18, SCREEN_WIDTH*0.18)];
    tiao.userInteractionEnabled = YES;
    tiao.backgroundColor = [UIColor clearColor];
    tiao.image = [UIImage imageNamed:@"tabbar_np_loop@2x.png"];
    tiao.center = CGPointMake(SCREEN_WIDTH*0.09, SCREEN_WIDTH*0.09);
    [self.btnBackground addSubview:tiao];
    
    //旋转ImageView
    self.musicBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.musicBtn.frame = CGRectMake(0, 0, SCREEN_WIDTH*0.14, SCREEN_WIDTH*0.14);
    self.musicBtn.backgroundColor = [UIColor blueColor];
    [self.musicBtn setImage:[UIImage imageNamed:@"tabbar_np_playshadow@2x.png"] forState:UIControlStateNormal];
//通知
    [self.musicBtn setBackgroundImage:[UIImage imageNamed:ImagePlaceHolder] forState:UIControlStateNormal];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(ChangeImage:) name:@"playImage" object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(musicStop:) name:@"musicStop" object:nil];
    
    self.musicBtn.layer.masksToBounds = YES;
    self.musicBtn.layer.cornerRadius = SCREEN_WIDTH*0.07;
//    self.musicBtn.layer.cornerRadius = SCREEN_WIDTH*0.075;
    self.musicBtn.center = CGPointMake(SCREEN_WIDTH*0.09, SCREEN_WIDTH*0.09);
    [tiao addSubview:self.musicBtn];
    
    //播放按钮
    self.playBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.playBtn.backgroundColor = [UIColor clearColor];
    self.playBtn.frame = CGRectMake(0, 0, SCREEN_WIDTH*0.18, SCREEN_WIDTH*0.18);
    self.playBtn.layer.masksToBounds = YES;
    self.playBtn.layer.cornerRadius = SCREEN_WIDTH*0.075;
    self.playBtn.center = CGPointMake(SCREEN_WIDTH*0.09, SCREEN_WIDTH*0.09);
    [self.playBtn setImage:[UIImage imageNamed:@"tabbar_np_play@2x.png"] forState:UIControlStateNormal];
    self.playBtn.alpha = 0.6;
    [tiao addSubview:self.playBtn];
    [self.playBtn addTarget:self action:@selector(playMusic:) forControlEvents:UIControlEventTouchUpInside];
    
    self.btnBackground.userInteractionEnabled = YES;
    tiao.userInteractionEnabled = YES;
}
-(void)ChangeImage:(NSNotification*)notify{
    LXAuthorDetailModel *temp = [notify object];
    UIImageView *tempImage = [UIImageView new];
    [tempImage sd_setImageWithURL:[NSURL URLWithString:temp.coverLarge] placeholderImage:[UIImage imageNamed:ImagePlaceHolder]];
    [self.musicBtn setImage:tempImage.image forState:UIControlStateNormal];
}
-(void)musicStop:(NSNotification *)notify{
    [self stopCircle];
}
//这个按钮是用来让暂停图标消失的
-(void)playMusic:(UIButton *)btn{
    [self startCircle];
}
-(void)startCircle{
#pragma mark - musicBtn旋转动画
    //改变view Z轴
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    //旋转的起始值 从零开始转
    animation.fromValue = [NSNumber numberWithInt:0];
    //最终旋转的角度是360°
    animation.toValue = [NSNumber numberWithInt:M_PI * 2];
    //旋转时长
    animation.duration = 5;
    //重复次数 0 为无限次
    animation.repeatCount = NSIntegerMax;
    //旋转结束后是否要逆向返回原位置
    animation.autoreverses = NO;
    //是否按照结束位置继续旋转
    animation.cumulative = YES;
    //给view的layer层添加动画,key是标记  随便起名字
    [self.musicBtn.layer addAnimation:animation forKey:@"circle"];
    self.musicBtn.layer.speed = 1.0;
    self.playBtn.alpha = 0;
    self.playBtn.enabled = NO;
}

-(void)stopCircle{
    self.musicBtn.layer.speed = 0;
    self.playBtn.alpha = 1;
    self.playBtn.enabled = YES;
}
@end
